package com.lg.system;


import com.lg.system.CDataPageBase;
import com.lg.system.CDateTime;
import com.lg.system.IDateFormat;
public class CPageDeal extends CDataPageBase
implements IDateFormat, java.io.Serializable{

	private String mModuleTitle ="";//模块的标题
	  private String TableFirstCss="alternate_line1";
	  private String TableSecondCss="alternate_line2";
	  private String LineMouseOverColor="#ECF6FB";
	  /**
	   * 设置打开的窗口显示的模块标题
	   * @param pModuleTitle
	   */
	  public void setModuleTitle(String pModuleTitle){
	    this.mModuleTitle = pModuleTitle;
	  }
	  /**
	   * 取回打开的窗口的模块标题
	   * @return
	   */
	  public String getModuleTitle(){
	    return this.mModuleTitle;
	  }

	  public CPageDeal() throws Exception{
//	    //使用唯一保持的数据库类
//	    super(CDBManager.getConn());
	  }

	  /**
	   * 初始化类
	   * @param pPageRowCount 每一页的记录数
	   * @param pCurPageNum 当前页码
	   */
	  public CPageDeal(int pPageRowCount, int pCurPage) throws Exception{
	    //使用唯一保持的数据库类
//	    super(CDBManager.getConn());
	    this.setPageRowCount(pPageRowCount);
	    this.setCurPage(pCurPage);
	  }

	  /**
	   * 获取日期类型纪录的字段值，格式按照egov中约定的格式到天
	   * @param pRowNum 当前纪录
	   * @param pFieldName 字段名称
	   * @return 字段值，NULL值返回空格
	   */

	  public String getFieldOfDay(int pRowNum, String pFieldName) {

	    Object obt = getObjectAt(pRowNum, pFieldName);
	    if (obt == null||obt.equals("")) {
	      return "";
	    }
	    //假如不是日期类型自动抛出异常
	    return obt.toString().substring(0,10);
	    //return CDateTime.format( (java.sql.Timestamp) obt,this.FORMATPAT_DAY);
	  }

	  /**
	   * 获取日期类型纪录的字段值，格式按照egov中约定的格式到分
	   * @param pRowNum 当前纪录
	   * @param pFieldName 字段名称
	   * @return 字段值，NULL值返回空格
	   */

	  public String getFieldOfMinute(int pRowNum, String pFieldName) {
	    Object obt = getObjectAt(pRowNum, pFieldName);
	    if (obt == null||obt.equals("")) {
	      return "";
	    }
	    //假如不是日期类型自动抛出异常
	    return obt.toString().substring(0,16);
	    //return CDateTime.format( (java.sql.Timestamp) obt,this.FORMATPAT_MINUTE);
	  }

	  /**
	   * 获取日期类型纪录的字段值，格式按照egov中约定的格式到分
	   * @param pRowNum 当前纪录
	   * @param pFieldName 字段名称
	   * @return 字段值，NULL值返回空格
	   */

	  public String getFieldOfSecond(int pRowNum, String pFieldName) {
	    Object obt = getObjectAt(pRowNum, pFieldName);
	    if (obt == null||obt.equals("")) {
	      return "";
	    }
	    //假如不是日期类型自动抛出异常
	    return obt.toString().substring(0,19);
	    //return CDateTime.format( (java.sql.Timestamp) obt,this.FORMATPAT_SECOND);
	  }

	  /**
	   * 获取纪录的字段值
	   * @param pRowNum 当前纪录
	   * @param pFieldName 字段名称
	   * @param pIsShowTitle 是否显示title
	   * @param pMaxLength 最长的字数，英文和数字两个算一个
	   * @return 字段显示格式，包含显示title
	   */

	  public String getFieldValue(int pRowNum, String pFieldName,
	                              boolean pIsShowTitle, int pMaxLength) {
	    String value = getFieldValue(pRowNum, pFieldName);
	    if (pIsShowTitle) {
	      value = "<div valign='center' title='" + value + "'>&nbsp" +
	          getMaxShowString(value, pMaxLength) + "</div>";
	    }
	    return value;
	  }

	  private String getMaxShowString(String pValue, int pMaxLength) {
	    if ( (pValue == null) || (pValue.trim().equals(""))) {
	      return pValue;
	    }
	    int intMaxLen = pMaxLength * 2;
	    StringBuffer strBuffer = new StringBuffer();
	    for (int i = 0; i < pValue.length(); i++) {
	      //假如是英文或数字，只能算半个
	      if ( (pValue.charAt(i) > 30) && (pValue.charAt(i) < 127)) {
	        intMaxLen = intMaxLen - 1;
	      }
	      else {
	        intMaxLen = intMaxLen - 2;
	      }

	      //最后半个中文字符不算
	      if (intMaxLen < 0) {
	        strBuffer.append("...");
	        break;
	      }
	      else {
	        strBuffer.append(pValue.charAt(i));
	      }
	    }
	    return strBuffer.toString();
	  }






	  /**
	   * 打印数据列表的每一行，不连接察看窗口
	   * @param pRowNum 当前纪录
	   * @return 每一行的代码
	   */
	  public String getTableTitle(int pRowNum) {
	    return getTableTitle(pRowNum, null, null, null);
	  }
	  /**
	   * 打印数据列表的每一行，带有察看窗口
	   * @param pRowNum 当前纪录
	   * @param pKeys 要作为参数的字段
	   * @param pLinkPage 打开的页面
	   * @return 每一行的代码
	   */
	  public String getTableTitle(int pRowNum, String[] pKeys, String pLinkPage) {
	    return getTableTitle(pRowNum, pKeys, pLinkPage, null);
	  }

	  /**
	   * 打印数据列表的每一行，带有察看窗口，可以待入不同的参数
	   * @param pRowNum 当前纪录
	   * @param pKeys 要作为参数的字段
	   * @param pLinkPage 打开的页面
	   * @param pStaticParam 静态参数
	   * @return 每一行的代码
	   */
	  public String getTableTitle(int pRowNum, String[] pKeys, String pLinkPage,
	                                 String pStaticParam) {
	    String columnStr = "";
	    String sCssName = TableSecondCss;
	    if (pRowNum % 2 == 0) {
	      sCssName = TableFirstCss;
	    }
	    if ( (pKeys == null) || (pKeys.length == 0)) {
	      columnStr = "<tr class='" + sCssName + "' >";
	    }
	    else {
	      for (int i = 0; i < pKeys.length; i++) {
	        columnStr += pKeys[i] + "=" + getFieldValue(pRowNum, pKeys[i]) + "&";
	      }
	      if ((pStaticParam == null )||(pStaticParam.trim().equals(""))) {
	        pStaticParam = "1=2";
	      }
	      columnStr = columnStr + pStaticParam+"&moduleTitle="+this.mModuleTitle;
	      columnStr = pLinkPage + "?" + columnStr;
	      columnStr = " onclick=\"openHalfWindow('" + columnStr + "')\" ";
	      columnStr = "<tr class='" + sCssName + "' " + columnStr +
	          "onmouseover=\"changeColor(this,1)\" onmouseout=\"changeColor(this,2)\" style='cursor:hand' >";
	    }
	    return columnStr;
	  }

	  /**
	     * 打印数据列表的每一行，带有处理窗口，可以待入不同的参数
	     * @param pRowNum 当前纪录
	     * @param pKeys 要作为参数的字段
	     * @param pLinkPage 打开的页面
	     * @param pStaticParam 静态参数
	     * @return 每一行的代码
	     */
	  public String getHandleTableTitle(int pRowNum, String[] pKeys, String pLinkPage,
	                                 String pStaticParam) {
	    String columnStr = "";
	    String sCssName = TableSecondCss;
	    if (pRowNum % 2 == 0) {
	      sCssName = TableFirstCss;
	    }
	    if ( (pKeys == null) || (pKeys.length == 0)) {
	      columnStr = "<tr class='" + sCssName + "' >";
	    }
	    else {
	      for (int i = 0; i < pKeys.length; i++) {
	        columnStr += pKeys[i] + "=" + getFieldValue(pRowNum, pKeys[i]) + "&";
	      }
	      if ((pStaticParam == null )||(pStaticParam.trim().equals(""))) {
	        pStaticParam = "1=2";
	      }
	      columnStr = columnStr + pStaticParam+"&moduleTitle="+this.mModuleTitle;
	      columnStr = pLinkPage + "?" + columnStr;
	      columnStr = " onclick=\"openNewWindow('" + columnStr + "',1,1)\" ";
	      columnStr = "<tr class='" + sCssName + "' " + columnStr +
	          "onmouseover=\"changeColor(this,1)\" onmouseout=\"changeColor(this,2)\" style='cursor:hand' >";
	    }
	    return columnStr;
	  }
	  /**
	     * 打印数据列表的每一行，带有处理窗口，可以待入不同的参数
	     * @param pRowNum 当前纪录
	     * @param pKeys 要作为参数的字段
	     * @param pLinkPage 打开的页面
	     * @param pStaticParam 静态参数
	     * @return 每一行的代码
	     */
	  public String getHrefTableTitle(int pRowNum, String[] pKeys, String pLinkPage,
	                                 String pStaticParam) {
	    String columnStr = "";
	    String sCssName = TableSecondCss;
	    if (pRowNum % 2 == 0) {
	      sCssName = TableFirstCss;
	    }
	    if ( (pKeys == null) || (pKeys.length == 0)) {
	      columnStr = "<tr class='" + sCssName + "' >";
	    }
	    else {
	      for (int i = 0; i < pKeys.length; i++) {
	        columnStr += pKeys[i] + "=" + getFieldValue(pRowNum, pKeys[i]) + "&";
	      }
	      if ((pStaticParam == null )||(pStaticParam.trim().equals(""))) {
	        pStaticParam = "1=2";
	      }
	      columnStr = columnStr + pStaticParam+"&moduleTitle="+this.mModuleTitle;
	      columnStr = pLinkPage + "?" + columnStr;
	      columnStr = " onclick=\"window.location.href='" + columnStr + "'\" ";
	      columnStr = "<tr class='" + sCssName + "' " + columnStr +
	          "onmouseover=\"changeColor(this,1)\" onmouseout=\"changeColor(this,2)\" style='cursor:hand' >";
	    }
	    return columnStr;
	  }
	  /**
	     * 打印数据列表的每一行，带有处理窗口，可以待入不同的参数
	     * 电子监察平台主要使用本方法，不再使用 getHrefTableTitle()
	     * @param pRowNum 当前纪录
	     * @param pKeys 要作为参数的字段
	     * @param pLinkPage 打开的页面
	     * @param pStaticParam 静态参数
	     * @return 每一行的代码
	     */
	  public String getHrefTableTitlex(int pRowNum,
	                                   String[] pKeys,
	                                   String pLinkPage,
	                                   String pStaticParam) {
	    return getHrefTableTitlex(pRowNum, pKeys, pLinkPage, pStaticParam, true);
	  }

	  /**
	     * 打印数据列表的每一行，带有处理窗口，可以待入不同的参数
	     * 电子监察平台主要使用本方法，不再使用 getHrefTableTitle()
	     * @param pRowNum 当前纪录
	     * @param pKeys 要作为参数的字段
	     * @param pLinkPage 打开的页面
	     * @param pStaticParam 静态参数
	     * @param isOpenNewWindow 是否使用全屏打开页面
	     * @return 每一行的代码
	     */
	  public String getHrefTableTitlex(int pRowNum,
	                                   String[] pKeys,
	                                   String pLinkPage,
	                                   String pStaticParam,
	                                   boolean isOpenNewWindow) {
	    String columnStr = "";
	    String sCssName = TableSecondCss;
	    if (pRowNum % 2 == 0) {
	      sCssName = TableFirstCss;
	    }
	    if ( (pKeys == null) || (pKeys.length == 0)) {
	      columnStr = "<tr class='" + sCssName + "' >";
	    }
	    else {
	      for (int i = 0; i < pKeys.length; i++) {
	        columnStr += pKeys[i] + "=" + getFieldValue(pRowNum, pKeys[i].toUpperCase()) + "&";
	      }
	      if ((pStaticParam == null )||(pStaticParam.trim().equals(""))) {
	        pStaticParam = "1=2";
	      }

	      columnStr = columnStr + pStaticParam;
	      if (isOpenNewWindow){
	        columnStr = "/system/common/handle.jsp?pageName=" + pLinkPage + "&" + columnStr;
	        columnStr = " onclick=\"openNewWindow('" + columnStr + "',0,1)\" ";
	      }
	      else{
	        columnStr = pLinkPage + "?" + columnStr;
	        columnStr = " onclick=\"window.location= '" + columnStr + "'\" ";
	      }
	      columnStr = "<tr class='" + sCssName + "' " + columnStr + "onmouseover=\"changeColor(this,1)\" onmouseout=\"changeColor(this,2)\" style='cursor:hand' >";

	    }
	    return columnStr;
	  }

	  /**
	     * 打印数据列表的每一行
	     * 电子监察平台主要使用本方法，不再使用 getHrefTableTitle()
	     * @param pRowNum 当前纪录
	     * @return 每一行的代码
	     */
	  public String getHrefTableTitlex(int pRowNum) {
	    String columnStr = "";
	    String sCssName = TableSecondCss;
	    if (pRowNum % 2 == 0) {
	      sCssName = TableFirstCss;
	    }
	    columnStr = "<tr class='" + sCssName + "' " + columnStr + " onmouseover=\"changeColor(this,1)\" onmouseout=\"changeColor(this,2)\" style='cursor:hand' >";
	    return columnStr;
	  }

	  public static void main(String[] args) throws Exception{
		  CPageDeal CDataPage1 = new CPageDeal();
	  }
	  public void setTableFirstCss(String TableFirstCss) {
	    this.TableFirstCss = TableFirstCss;
	  }
	  public void setTableSecondCss(String TableSecondCss) {
	    this.TableSecondCss = TableSecondCss;
	  }
	  private String sql_select_str;     // SQL 的组成部分，SELECT
	  private String[] sql_select_array = null; // SQL 的组成部分，SELECT
	  private String[] sql_from = null;         // SQL 的组成部分，FROM
	  private String sql_where;          // SQL 的组成部分，WHERE
	  private String sql_order;          // SQL 的组成部分，ORDER
	  public void setSql_select(String sql_select_str){
	    this.sql_select_str = sql_select_str;
	  }
	  public void setSql_select(String[] sql_select_array){
	    this.sql_select_array = sql_select_array;
	  }
	  public void setSql_from(String[] sql_from){
	    this.sql_from = sql_from;
	  }
	  public void setSql_where(String sql_where){
	    this.sql_where = sql_where;
	  }
	  public void setSql_order(String sql_order){
	    this.sql_order = sql_order;
	  }
	  public String makeSqlWithSqlPart() throws Exception{
	    if ((sql_select_str == null && sql_select_array == null) || sql_from == null){
	      throw new Exception("查询语句必须包括 SELECT 和 FROM 部分。");
	    }
	    if (sql_select_array != null && sql_select_array.length != sql_from.length){
	      throw new Exception("SELECT 部分数组长度必须和 FROM 部分数组长度一致");
	    }
	    String[] sSqls = new String[sql_from.length];

	    for (int i = 0; i < sSqls.length; i++){
	      if (sql_select_array != null){
	        sSqls[i] = "SELECT " + sql_select_array[i];
	      }
	      else{
	        sSqls[i] = "SELECT " + sql_select_str;
	      }
	      sSqls[i] = sSqls[i] + " FROM " + sql_from[i];
	      if (sql_where != null){
	        sSqls[i] = sSqls[i] + " WHERE " + sql_where;
	      }
	    }
	    StringBuffer sbSQL = new StringBuffer(100);
	    for (int i = 0; i < sSqls.length; i++){
	      sbSQL.append(sSqls[i]);
	      if (i < sSqls.length - 1){
	        sbSQL.append(" UNION ALL ");
	      }
	    }
	    super.setMCountSQL("SELECT COUNT(1) FROM (" + sbSQL.toString() + ")");
	    if (sql_order != null){
	      sbSQL.append(" ").append(sql_order);
	    }
	    super.setQuerySql(sbSQL.toString());
	    return super.getQuerySql();
	  }
	/* 以上由 陈伟佳 20050513 增加*/

	  public String getTurnPage(int pLocation){
	    String mFormName = super.getFormName();
	    mFormName = mFormName.replaceAll("\\[", "");
	    mFormName = mFormName.replaceAll("\\]", "");

	    StringBuffer sb = new StringBuffer(100);
	    sb.append("\n");
	    sb.append("<TABLE cellSpacing=0 cellPadding=0 border=0>\n");
	    sb.append("  <TBODY>\n");
	    sb.append("    <TR>\n");
	    sb.append("      <TD class='td_Page'> 转到第</TD>\n");
	    sb.append("      <TD>\n");
	    sb.append("        <input name='jump_to_page_no_").append(pLocation).append("' type='text' class='input' size='3'>\n");
	    sb.append("      </TD>\n");
	    sb.append("      <TD><span class='td_Page'>页</span></TD>\n");
	    sb.append("      <TD>\n");
	    sb.append("        <input type='button' class='button_page_go' value='' onclick='click_jump_button(document.").append(super.getFormName()).append(".jump_to_page_no_").append(pLocation).append(",document.").append(super.getFormName()).append(",document.").append(super.getFormName()).append(".selectpageno").append(mFormName).append(")' style='cursor:hand' alt=''>\n");
	    sb.append("      </TD>\n");
	    sb.append("      <TD>&nbsp;&nbsp;&nbsp;</TD>\n");

	    int iStartPage = super.getCurPage() - 4;
	    if (iStartPage < PAGE_FIRST)
	      iStartPage = PAGE_FIRST;

	    int iEndPage = iStartPage + 9;
	    if (iEndPage > super.getTotalPage())
	      iEndPage = super.getTotalPage();

	    iStartPage = iEndPage - 9;
	    if (iStartPage < PAGE_FIRST)
	      iStartPage = PAGE_FIRST;

	    if (super.getCurPage() > 1){
	      sb.append("      <TD><img src='/images/zjm/BtnPagePrev2.gif' width='25' height='20' onclick='click_jump_to_image(1,document.").append(super.getFormName()).append(",document.").append(super.getFormName()).append(".selectpageno").append(mFormName).append(")' style='cursor:hand'><IMG src='' height=1 width=5></TD>\n");
	      sb.append("      <TD><img src='/images/zjm/BtnPagePrev1.gif' width='25' height='20' onclick='click_move_to_image(-1,document.").append(super.getFormName()).append(",document.").append(super.getFormName()).append(".selectpageno").append(mFormName).append(")' style='cursor:hand'><IMG src='' height=1 width=5></TD>\n");
	    }
	    else{
	      sb.append("      <TD><img src='/images/zjm/BtnPagePrev22.gif' width='25' height='20' onclick=''><IMG src='' height=1 width=5></TD>\n");
	      sb.append("      <TD><img src='/images/zjm/BtnPagePrev12.gif' width='25' height='20' onclick=''><IMG src='' height=1 width=5></TD>\n");
	    }
	    sb.append("      <TD>\n");

	    for (int i = iStartPage; i <= iEndPage; i++){
	      if (i == super.getCurPage()){
	        sb.append("        <SPAN class=TurnPage title=当前为第").append(i).append("页>").append(i).append("</SPAN>\n");
	      }
	      else{
	        sb.append("        <A class=TurnPage title=跳到第").append(i).append("页 href='javascript:click_jump_to_image(").append(i).append(",document.").append(super.getFormName()).append(",document.").append(super.getFormName()).append(".selectpageno").append(mFormName).append(")'>").append(i).append("</A>\n");
	      }
	    }
	    sb.append("      &nbsp;</TD>\n");
	    if (super.getCurPage() < super.getTotalPage()){
	      sb.append("      <TD><img src='/images/zjm/BtnPageNext1.gif' width='25' height='20' onclick='click_move_to_image(1,document.").append(super.getFormName()).append(",document.").append(super.getFormName()).append(".selectpageno").append(mFormName).append(")' style='cursor:hand'><IMG src='' height=1 width=5></TD>\n");
	      sb.append("      <TD><img src='/images/zjm/BtnPageNext2.gif' width='25' height='20' onclick='click_jump_to_image(").append(super.getTotalPage()).append(",document.").append(super.getFormName()).append(",document.").append(super.getFormName()).append(".selectpageno").append(mFormName).append(")' style='cursor:hand'><IMG src='' height=1 width=5></TD>\n");
	    }
	    else{
	      sb.append("      <TD><img src='/images/zjm/BtnPageNext12.gif' width='25' height='20' onclick=''><IMG src='' height=1 width=5></TD>\n");
	      sb.append("      <TD><img src='/images/zjm/BtnPageNext22.gif' width='25' height='20' onclick=''><IMG src='' height=1 width=5></TD>\n");
	    }

	    sb.append("    </TR>\n");
	    sb.append("  </TBODY>\n");
	    sb.append("</TABLE>\n");

	    return  sb.toString();
	  }

	  public String getTurnPageInfo(){
	    //<span class="td_Page">当前为第1页，共3页<strong>22</strong>条记录 </span>
	    String mFormName = super.getFormName();
	    mFormName = mFormName.replaceAll("\\[", "");
	    mFormName = mFormName.replaceAll("\\]", "");
	    StringBuffer sb = new StringBuffer(30);
	    sb.append("<SCRIPT>function changeColor(obj,tag){if(obj.cells.length < 1 ){ return false; }if(tag == 1){document.").append(super.getFormName()).append(".tdoriginalcolor.value = obj.cells(0).bgColor;for (i=0; i < obj.cells.length; i++){obj.cells(i).bgColor = '").append(LineMouseOverColor).append("';}}else{for (i=0; i < obj.cells.length; i++){obj.cells(i).bgColor = document.").append(super.getFormName()).append(".tdoriginalcolor.value;}} }</SCRIPT>\n");
	    sb.append("<input type='hidden' name='selectpageno").append(mFormName).append("' value='").append(super.getCurPage()).append("'>\n");
	    sb.append("<input type='hidden' name='tdoriginalcolor' value=''>\n");
	    sb.append("<span class='td_Page'>当前为第");
	    sb.append(super.getCurPage());
	    sb.append("页，共");
	    sb.append(super.getTotalPage());
	    sb.append("页<strong>");
	    sb.append(super.getTotalRowCount());
	    sb.append("</strong>条记录</span>");
	    return sb.toString();
	  }
}
